package com.saodisheng.service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.saodisheng.model.Posts;
import com.saodisheng.service.AccountService;
import com.saodisheng.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * description:
 *
 * @author 扫地生_saodisheng
 * @date 2022/1/28
 */
@Service
public class ViewServiceImpl implements ViewService {
	@Autowired
	private AccountService accountService;

	final Posts postDao = new Posts().dao();
	
	@Override
	public Posts getPost(int id) {
		Posts post = postDao.findById(id);
		accountService.join("userId", post, "nickName", "avatar");
		return post;
	}

	@Override
	public void updateViews(int id) {
		Db.update("update posts set views=views+1 where id=?", id);
		return;
	}

	@Override
	public int getPostNum(int userId) {
		Number postNum = Db.queryNumber("select count(*) from posts where userId=?", userId);
		
		return postNum.intValue();
	}
	
	@Override
	public int getCommentNum(int userId) {
		Number commentNum = Db.queryNumber("select count(*) from posts_comment where userId=?", userId);
		return commentNum.intValue();
	}
}
